import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:simple_app/common/global.dart';
import 'package:simple_app/common/storage_util.dart';
import 'package:simple_app/main.dart';

class UserInfo {
  final String id;
  final String name;
  UserInfo({required this.id, required this.name});
}

//D:\hqk\projects\study-nodejs\ws-server-chat 服务器在这

class Api {
  static String token = '';

  static UserInfo userInfo = UserInfo(id: '', name: '');

  Future<String> setToken(String v) async {
    var instance = await SharedPreferences.getInstance();
    instance.setString('token', v);
    token = v;
    return token;
  }

  Future<String> getToken() async {
    if (token == '') {
      try {
        var instance = await SharedPreferences.getInstance();
        token = instance.getString('token')!;
      } catch (e) {
        return '';
      }
      return token;
    }
    return token;
  }

  late var baseurl = '';
  Api() {
    baseurl = 'http://127.0.0.1:3000';
  }

  //若jsonstr为空则直接使用上次请求的结果进行json转换
  json({String jsonstr = ''}) {
    return jsonDecode(jsonstr);
  }

  //获取最近的交流信息
  Future<http.Response> chatMessageList(String friendid) async {
    var token1 = await getToken();
    return http
        .get(Uri.parse("$baseurl/message?token=$token1&friendid=$friendid"));
  }

  //登录
  Future<http.Response?> login(String user, String pwd) async {
    try {
      var user1 = Uri.encodeComponent(user);
      var pwd1 = Uri.encodeComponent(pwd);
      var response =
          await http.get(Uri.parse("$baseurl/login?&user=$user1&pwd=$pwd1"));
      return response;
    } catch (e) {
      return null;
    }
  }

  Future<http.Response?> get(url) async {
    try {
      var response = await http.get(Uri.parse(url));
      if (response.statusCode != 200) {
        //置空本地信息
        Api.token = '';
        await StorageUtil.putString('token', '');
        await StorageUtil.putString('nickname', '');
        await StorageUtil.putString('user', '');
        await StorageUtil.putString('pwd', '');
        Global.closeWebSocket();
        MyApp.navKey.currentState?.popAndPushNamed('/login');
        return null;
      }
      return response;
    } catch (e) {
      //置空本地信息
      Api.token = '';
      await StorageUtil.putString('token', '');
      await StorageUtil.putString('nickname', '');
      await StorageUtil.putString('user', '');
      await StorageUtil.putString('pwd', '');
      Global.closeWebSocket();
      MyApp.navKey.currentState?.popAndPushNamed('/login');
      return null;
    }
  }

  Future<http.Response?> post(String url, dynamic data) async {
    try {
      var response = await http.post(Uri.parse(url), body: data);
      if (response.statusCode != 200) {
        //置空本地信息
        Api.token = '';
        await StorageUtil.putString('token', '');
        await StorageUtil.putString('nickname', '');
        await StorageUtil.putString('user', '');
        await StorageUtil.putString('pwd', '');
        Global.closeWebSocket();
        MyApp.navKey.currentState?.popAndPushNamed('/login');
        return null;
      }
      return response;
    } catch (e) {
      MyApp.navKey.currentState?.popAndPushNamed('/login');
      return null;
    }
  }

  Future<http.Response?> friendFind(String keyword) async {
    var token1 = await getToken();
    return get("$baseurl/find?token=$token1&keyword=$keyword");
  }

  //申请加好友
  Future<http.Response?> invite(String acceptor) async {
    var token1 = await getToken();
    return get("$baseurl/invite?token=$token1&acceptor=$acceptor");
  }

  //拒绝
  Future<http.Response?> deny(String acceptid) async {
    var token1 = await getToken();
    return get("$baseurl/deny?token=$token1&acceptid=$acceptid");
  }

  //同意
  Future<http.Response?> accept(String acceptid) async {
    var token1 = await getToken();
    return get("$baseurl/accept?token=$token1&acceptid=$acceptid");
  }

  //申请列表
  Future<http.Response?> acceptList() async {
    var token1 = await getToken();
    return get("$baseurl/accept_list?token=$token1");
  }

  //好友列表
  Future<http.Response?> friendList() async {
    var token1 = await getToken();
    return get("$baseurl/friend_list?token=$token1");
  }

  //信息列表
  Future<http.Response?> messageList() async {
    var token1 = await getToken();
    return get("$baseurl/message_list?token=$token1");
  }

  //注册
  Future<http.Response?> reg(String user, String pwd, String name) async {
    var token1 = await getToken();
    return post(
        "$baseurl/reg?token=$token1", {"user": user, "pwd": pwd, "name": name});
  }
}
